<?php 
	
	if($demand->course_id !== NULL)
		$course = $demand->course();
	else
		$course = NULL;
	
	if(!is_null($course))
		$traininggroup = $course->traininggroup();
	else
		$traininggroup = FALSE;
		
	$active_training = NULL;
	if($traininggroup)
	{

		$traininggroup = $traininggroup[0];
		
		foreach($traininggroup->training() as $training)
		{
			if($training->canceled == '0' && $training->trainingstatus()->name !== 'Storniert')
				$active_training = $training;
		}
	}
	$tag = $demand->tag();

	if(!$demand->inactive)
	{
		echo '<div class="well well-custom blocked">';
	} 
    else if($traininggroup && !$active_training)
    {
    	echo '<div class="canceld well-custom">';
    }
    else {
    	echo '<div class="magenta well-custom">';
    }
	
	/**
	 * Show the name of the StoPE, course or measure
	 * Priority: Measure > Course > StoPE
	 * In case of Stope: Link nowhere
	 * In case of Course: Link to course-info
	 * In case of measure: link to measure-info
	 */
	 
	 
	
	if($course && strlen(($course->title)) > 20 && isset($course))
	{
		$link_name = substr($course->title,0,20).'...';
	}
	else if(isset($course))
	{
		$link_name = $course->title;
	}
		
	
	if($active_training)
	{
		echo anchor('training/details/'.$active_training->id,$link_name);
	}
	else if($course)
	{
		echo anchor('catalogue/details/'.$course->id,$link_name);
	}
	else
	{
		if(strlen(($demand->title)) > 20)
			echo $link_name = substr($demand->title,0,20).'...';
		else
			echo $demand->title;				
	}
	
	if('STR' === $tag->name)
	{
		echo '<span class="badge-inhouse label label-success">'.$tag->name.'</span>';
	}
	
	if($active_training)
	{
		$status = $active_training->trainingstatus();
		switch($status->name)
		{
			case 'Festgelegt':
			case 'Geschlossen':
			case 'Schwebend': echo '<div class="dot dot-box dot-blue"></div>'; break;
			case 'Gebucht': echo '<p class="dot dot-box dot-orange"></p>'; break;
			case 'Fakturiert': echo '<p class="dot dot-box dot-green"></p>'; break;
			default: ;//echo $status->name; break;
		}
	}
	echo '<br>';
	
	/**
	 * Show the price of the Course / Measure
	 * Priority: Measure > Course
	 */			
	if($active_training)
	{
		$amount = NULL;
		$cost_arr = $traininggroup->cost();
		foreach($cost_arr as $cost ) {
			$amount += $cost->amount;
		}
		if($amount)
		{
			echo '<span>Einzelpreis: '.number_format ($amount, 2, ',' , '.' ).'€</span>';
		}
		else
		{
			echo '<span>Einzelpreis: N/A </span>';
		}
	}
	else if($course)
	{
		$cost = $course->cost();

		echo '<span>Schulungspreis:'.number_format ( $cost->amount, 2, ',' , '.' ).'€</span>';
		
	}
	else
	{
		echo '<span>&nbsp</span>';
	}
	//else show nothing
	
	
	/**
	 * Show button to activate or deactivate the SToPE
	 */
		
	
	if(!$demand->inactive)
	{
		echo '</div>';
	}
	//else show nothing
			
			
	/*
		<td>
			<p class="dot dot-box dot-blue"></p>
			<a href="">Reality Training</a> 
			<span class="badge-inhouse label label-success">STR</span><br >
			
			<a href="" class="action-icon"><i class="icon-minus-sign"></i>
		</td>
	*/
		
		

		